home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / emulator / bsvc-1.000 / bsvc-1 / bsvc-1.0.4 / src / Assemblers / 68kasm / instlookup.c < prev    next >
C/C++ Source or Header  |  1995-07-26  |  3KB  |  123 lines

  1. /******************************************************************************
  2.  * $Id: instlookup.c,v 1.1 1994/08/29 23:59:42 bmott Exp $
  3.  ******************************************************************************
  4.  *
  5.  *        INSTLOOKUP.C
  6.  *        Instruction Table Lookup Routine for 68000 Assembler
  7.  *
  8.  *    Function: instLookup()
  9.  *        Parses an instruction and looks it up in the instruction
  10.  *        table. The input to the function is a pointer to the
  11.  *        instruction on a line of assembly code. The routine 
  12.  *        scans the instruction and notes the size code if 
  13.  *        present. It then (binary) searches the instruction 
  14.  *        table for the specified opcode. If it finds the opcode, 
  15.  *        it returns a pointer to the instruction table entry for 
  16.  *        that instruction (via the instPtrPtr argument) as well 
  17.  *        as the size code or 0 if no size was specified (via the 
  18.  *        sizePtr argument). If the opcode is not in the 
  19.  *        instruction table, then the routine returns INV_OPCODE. 
  20.  *        The routine returns an error value via the standard
  21.  *        mechanism. 
  22.  *
  23.  *     Usage:    char *instLookup(p, instPtrPtr, sizePtr, errorPtr)
  24.  *        char *p;
  25.  *        instruction *(*instPtrPtr);
  26.  *        char *sizePtr;
  27.  *        int *errorPtr;
  28.  *
  29.  *    Errors: SYNTAX
  30.  *        INV_OPCODE
  31.  *        INV_SIZE_CODE
  32.  *
  33.  *      Author: Paul McKee
  34.  *        ECE492    North Carolina State University
  35.  *
  36.  *        Date:    9/24/86
  37.  *
  38.  *   Copyright 1990-1991 North Carolina State University. All Rights Reserved.
  39.  *
  40.  ******************************************************************************
  41.  * $Log: instlookup.c,v $
  42.  * Revision 1.1  1994/08/29  23:59:42  bmott
  43.  * Initial revision
  44.  *
  45.  *****************************************************************************/
  46.  
  47.  
  48. #include <stdio.h>
  49. #include <ctype.h>
  50. #include "asm.h"
  51.  
  52.  
  53. extern instruction instTable[];
  54. extern int tableSize;
  55.  
  56.  
  57. char *instLookup(p, instPtrPtr, sizePtr, errorPtr)
  58. char *p;
  59. instruction *(*instPtrPtr);
  60. char *sizePtr;
  61. int *errorPtr;
  62. {
  63. char opcode[8];
  64. int i, hi, lo, mid, cmp;
  65.  
  66. /*    printf("InstLookup: Input string is \"%s\"\n", p); */
  67.     i = 0;
  68.     do {
  69.         if (i < 7)
  70.             opcode[i++] = *p;
  71.         p++;
  72.     } while (isalpha(*p));
  73.     opcode[i] = '\0';
  74.     if (*p == '.')
  75.         if (isspace(p[2]) || !p[2]) {
  76.             if (p[1] == 'B')
  77.                 *sizePtr = BYTE;
  78.             else if (p[1] == 'W')
  79.                 *sizePtr = WORD;
  80.             else if (p[1] == 'L')
  81.                 *sizePtr = LONG;
  82.             else if (p[1] == 'S')
  83.                 *sizePtr = SHORT;
  84.             else {
  85.                 *sizePtr = 0;
  86.                 NEWERROR(*errorPtr, INV_SIZE_CODE);
  87.                 }
  88.             p += 2;
  89.             }
  90.         else {
  91.             NEWERROR(*errorPtr, SYNTAX);
  92.             return NULL;
  93.             }
  94.     else if (!isspace(*p) && *p) {
  95.         NEWERROR(*errorPtr, SYNTAX);
  96.         return NULL;
  97.         }
  98.     else
  99.         *sizePtr = 0;
  100.  
  101.     lo = 0;
  102.     hi = tableSize - 1;
  103. #ifdef TAN_DEBUG
  104.     printf("hi=%d, tableSize=%d\n", hi, tableSize);
  105. #endif TAN_DEBUG
  106.     do {
  107.         mid = (hi + lo) / 2;
  108.         cmp = strcmp(opcode, instTable[mid].mnemonic);
  109.         if (cmp > 0)
  110.             lo = mid + 1;
  111.         else if (cmp < 0)
  112.             hi = mid - 1;
  113.     } while (cmp && (hi >= lo));
  114.     if (!cmp) {
  115.         *instPtrPtr = &instTable[mid];
  116.         return p;
  117.         }
  118.     else {
  119.         NEWERROR(*errorPtr, INV_OPCODE);
  120.         return NULL;
  121.         }
  122. }
  123.